package SpringFramework.计划任务;

import java.util.Date;
import java.util.concurrent.ScheduledFuture;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;

import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;
import com.titan.core.utils.thread.ThreadUtils;


public class Main_方法 {
    private static void run() throws Exception {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(1);
        taskScheduler.initialize();  // 必须得先初始化，才能使用
        
        // 
        ScheduledFuture<?> future1 = taskScheduler.schedule(createRun1(), new CronTrigger("0/1 * * * * ?"));
        ScheduledFuture<?> future2 = taskScheduler.schedule(createRun2(), new CronTrigger("0/2 * * * * ?"));
        ScheduledFuture<?> future3 = taskScheduler.schedule(createRun3(), new CronTrigger("0/3 * * * * ?"));
        
        ThreadUtils.sleep(10000);
        future1.cancel(true);
        ConsoleUtils.log("future1.isCancelled", future1.isCancelled());
        ConsoleUtils.log("future2.isCancelled", future2.isCancelled());
        ConsoleUtils.log("future3.isCancelled", future3.isCancelled());
        
        ThreadUtils.sleep(10000);
        future2.cancel(true);
        ConsoleUtils.log("future1.isCancelled", future1.isCancelled());
        ConsoleUtils.log("future2.isCancelled", future2.isCancelled());
        ConsoleUtils.log("future3.isCancelled", future3.isCancelled());
        
        ThreadUtils.sleep(10000);
        future3.cancel(true);
        ConsoleUtils.log("future1.isCancelled", future1.isCancelled());
        ConsoleUtils.log("future2.isCancelled", future2.isCancelled());
        ConsoleUtils.log("future3.isCancelled", future3.isCancelled());
        
        ThreadUtils.sleep(2000);
        taskScheduler.shutdown();
    } 
    private final static Runnable createRun1() {
        return new Runnable() {
            public void run() {
                ConsoleUtils.log("1");
            }
        };
    }
    private final static Runnable createRun2() {
        return new Runnable() {
            public void run() {
                ConsoleUtils.log("2");
            }
        };
    }
    private final static Runnable createRun3() {
        return new Runnable() {
            public void run() {
                ConsoleUtils.log("3");
            }
        };
    }

    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print();
        }
    }

}
